// HSS.Net.Mail.MailCommand.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       MailCommand.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Net.Mail
{
	#region Using Directives
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Net.Mail;
	#endregion

	#region MailCommand
	/// <summary>
	/// Used to send the Mail command to an smtp server
	/// </summary>
	static class MailCommand
	{
		#region Fields
		static SmtpStatusCode ResponseCode = SmtpStatusCode.GeneralFailure;
		static string Response = string.Empty;
		#endregion

		#region Methods
		internal static void Send(SmtpConnection smtpConn, System.Net.Mail.MailAddress from)
		{

			MailCommand.PrepareCommand(smtpConn, from);

			smtpConn.Send();

			smtpConn.GetResponse(out ResponseCode, out Response);

			MailCommand.CheckResponse(ResponseCode, Response);

		}
		private static void PrepareCommand(SmtpConnection smtpConn, System.Net.Mail.MailAddress from)
		{
			ResponseCode = SmtpStatusCode.GeneralFailure;
			Response = string.Empty;

			string fromAddress = string.Empty;

			if (!from.Address.StartsWith("<"))
				fromAddress = "<" + from.Address;
			else
				fromAddress = from.Address;

			if (!from.Address.EndsWith(">"))
				fromAddress = fromAddress + ">";


			smtpConn.Buffer.Append(SmtpCommands.Mail);
			smtpConn.Buffer.Append(fromAddress);
			smtpConn.Buffer.Append(SmtpCommands.CRLF);

		}
		private static void CheckResponse(SmtpStatusCode code, string response)
		{
			if (code != SmtpStatusCode.Ok)
			{
				throw new SmtpException(code, response);
			}
		}
		#endregion
	}
	#endregion
}